import pandas as pd
from pandas import DataFrame
from config import Sensor, ProjectInfo


# region 内部处理函数
def t_merge_sensor_data(df_sensor_data: DataFrame):
    """
        计算各个传感器获取数据的平均值
    """

    df_sensor_data['d_time'] = pd.to_datetime(df_sensor_data['d_time'])
    df_mean = df_sensor_data.groupby(by="sensor_code").mean().round(2)
    df_mean['sensor_code'] = df_mean.index
    ProjectInfo.sensor_code_list = df_mean['sensor_code'].to_list()
    ProjectInfo.sensor_count = len(ProjectInfo.sensor_code_list)
    new_sensor_code_list = set(ProjectInfo.sensor_code_list) - set(ProjectInfo.sensor_info_dict.keys())
    for sensor_code in new_sensor_code_list:
        ProjectInfo.sensor_info_dict[sensor_code] = Sensor(sensor_code=sensor_code)
    return df_mean


def get_sensor_base_info_df():
    """
    获取基础传感器信息并转为df
    :return:
    """
    sensor_base_info_dict_list = []
    for sensor_code in ProjectInfo.sensor_code_list:
        sensor = ProjectInfo.sensor_info_dict[sensor_code]
        sensor_base_info_dict_list.append({
            'id': sensor.id,
            'sensor_code': sensor.sensor_code,
            'location': sensor.location,
            'direction': sensor.direction,
            'product_id': sensor.product_id,
        })
    ProjectInfo.sensor_base_info_df = pd.DataFrame(sensor_base_info_dict_list)


# endregion


# region 对外接口函数
def get_data_dict_list_mean(data_dict_list: list):
    if not data_dict_list:
        return None
    df = pd.DataFrame(data_dict_list)
    return t_merge_sensor_data(df)


def df_mean_to_value(df_mean: DataFrame):
    base_df = ProjectInfo.sensor_base_info_df
    a_sensor_code_list = ProjectInfo.a_sensor_code_list
    b_sensor_code_list = ProjectInfo.b_sensor_code_list
    angle_a_df = df_mean[df_mean['sensor_code'].isin(a_sensor_code_list) ]
    angle_b_df = df_mean[df_mean['sensor_code'].isin(b_sensor_code_list) ]
    a_data = get_angle_acc(angle_a_df)
    b_data = get_angle_acc(angle_b_df)
    if base_df.empty:
        ProjectInfo.sensor_base_info_df = df_mean
        ProjectInfo.Base_Angle_Info["a"] = a_data
        ProjectInfo.Base_Angle_Info["b"] = b_data

    return {"a": a_data, "b": b_data}


def get_angle_acc(df):
    data_dict_list = df.to_dict('records')

    if not data_dict_list:
        return None

    data_dict = data_dict_list[0]
    return {"angle_x": data_dict["angle_x"], "angle_y": data_dict["angle_y"], "angle_z": data_dict["angle_z"],
            "acc_x": data_dict["acc_x"], "acc_y": data_dict["acc_y"], "acc_z": data_dict["acc_z"]}


def calculate_angle(data_dict: dict):
    base_data = ProjectInfo.Base_Angle_Info
    a_base_angle = base_data["a"]
    b_base_angle = base_data["b"]
    a_angel = data_dict["a"]
    b_angel = data_dict["b"]
    a_agree_list = [abs(a_angel["angle_x"] - a_base_angle["angle_x"]),
                    abs(a_angel["angle_y"] - a_base_angle["angle_y"]),
                    # abs(a_angel["angle_z"] - a_base_angle["angle_z"])
                    ]
    b_agree_list = [abs(b_angel["angle_x"] - b_base_angle["angle_x"]),
                    abs(b_angel["angle_y"] - b_base_angle["angle_y"]),
                    # abs(b_angel["angle_z"] - b_base_angle["angle_z"])
                    ]
    a_angel_max = max(a_agree_list)
    b_angel_max = max(b_agree_list)

    return {"a_angel": a_angel_max, "b_angel": b_angel_max, "total_angle": a_angel_max + b_angel_max}

# endregion
